$(function(){
    var flag=true;
var agentInfo=common.getLocalData('H5-agentInfo');
var $loadingFail=$('.loading-fail');
var $todayList=$('.today-list');
var token=common.getToken();
var documentHeight=0;
var windowHeight=0;
var $resMsg=$('#res-msg');
var url=common.globalHost+common.getLockOpenList;
var sendData={
    pageNum:1,
    numPerPage:10,
    token:token,
    agentId:agentInfo.id
};
requestData();
function buildLookHouseList(options){
    var dataList=options.dataList||[];
    var $loading=$(options.loadingSelector);
    var arrTemp=[];
    var dataCount=0;
    dataList.length<=sendData.numPerPage?$loading.text("已经没有更多信息了").show():"";
    $.each(dataList,function(idx,obj){
        dataCount++;
        var currentDate=obj['createTime']?obj['createTime']:"";
        var villageName=obj['villageName']?obj['villageName']:"";
        var houseAddress=obj['houseAddress']?obj['houseAddress']:"";
        if(!!obj['createTime']){
            currentDate = currentDate.replace(/[-.]/g,"/");
            var timePublishDate=new Date(currentDate);
            var fullYear = timePublishDate.getFullYear();
            var month = timePublishDate.getMonth()+1;
            var day = timePublishDate.getDate();
            currentDate=fullYear+"年"+month+"月"+day+"日";
        }
        var tempDom='<li>'+
                        '<div class="clearfix">'+
                            '<span class="house-name">'+villageName+'</span><span class="apply-time">'+currentDate+'</span>'+
                        '</div>'+
                        '<p>'+houseAddress+'</p>'+
                    '</li>';
        arrTemp.push(tempDom);
    });
    return arrTemp.join("");
};
$(window).on("scroll",function (e) {
    windowHeight=$(window).height();
    if($(this).scrollTop()+windowHeight==documentHeight){
        if(flag){
            requestData();
        };
    }
});
function requestData() {
    $.ajax({
        data:sendData,
        type: "POST",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        dataType:"json",
        url:url,
        crossDomain:true,
        success:function(data){
            if(typeof data==="string"){
                data=JSON.parse(data);
            }
            if(data.code==101 ||data.code==102){
                console.log("token为空或者token失效");
                window.location.href='login.html';
            };
            $resMsg.text("").hide();
            $loadingFail.hide();
            if(!!data && data['code']=="0"){
                var totalNum=data.res.data.page.recordCount;
                if(totalNum==0){
                    $loadingFail.show();
                }else{
                    data=data.res.data.record;
                    if(!data.length){
                        $resMsg.text("已经没有更多信息了").show();
                        flag=false;
                    }else{
                        var responseDom=buildLookHouseList({
                            loadingSelector:$resMsg,
                            dataList:data
                        });
                        $todayList.append(responseDom);
                        documentHeight=$(document).height();
                        sendData.pageNum++;
                        flag=true;
                    };
                };
            };
        },
        timeout:10000,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            common.errDialog("网络不畅，请稍后重试");  
        }
    });
};
})